<?php

require_once './config.php';

$c = mysql_connect(HOST, USER, PWD) or die("Connexion impossible au serveur");
mysql_select_db(DB) or die("Impossible de se connecter à la base de données");

$field = isset($_POST['field']) ? $_POST['field'] : 'id';
$order = isset($_POST['order']) ? $_POST['order'] : 'DESC';

$newOrder = ($order == 'DESC') ? 'ASC' : 'DESC';

$sql = "SELECT * FROM statistiques ORDER BY $field $order";

$result = mysql_query($sql) or die(mysql_error());

?>

<table class="table table-bordered table-condensed table-striped table-hover">
    <thead>
        <tr>
            <td>
                <a href='#' class='reorder-email' data-field='id' data-order='<?php echo $newOrder ?>'>ID</a>
            </td>
            <td>
                <a href='#' class='reorder-email' data-field='nom_newsletter' data-order='<?php echo $newOrder ?>'>NewsLetter</a>
            </td>
            <td>
                <a href='#' class='reorder-email' data-field='email' data-order='<?php echo $newOrder ?>'>Email</a>
            </td>
            <td>
                <a href='#' class='reorder-email' data-field='somme' data-order='<?php echo $newOrder ?>'>Nombre de consultation</a>
            </td>
        </tr>
    </thead>
    <tbody>
    <?php while($e = mysql_fetch_assoc($result)): ?>
        <tr>
            <td><?php echo $e['id'] ?></td>
            <td><?php echo $e['nom_newsletter'] ?></td>
            <td><?php echo $e['email'] ?></td>
            <td><?php echo $e['nb_ouverture'] ?></td>
        </tr>
    <?php endwhile; ?>
    </tbody>
</table>

<?php mysql_close(); ?>